<?php
namespace app\app\controller;
use think\Controller;
use think\Db;
class Pay extends Controller{	//支付授权
	public function _empty($name){
        die('非法访问！你的IP已被监控！');
    }
	
	public function shoppay(){	//商城支付
        $appdata=apijieshou();
        $setting=setting();
		//合法性
		if(!request()->isPost()){
			apifasong(['zt'=>'0','msg'=>'非法访问']);
		}
		if(!isset($appdata['ome']) && !isset($appdata['quan']) && !isset($appdata['kuang']) && !isset($appdata['jifen'])){
			apifasong(['zt'=>'0','msg'=>'非法扣款额度']);
		}
		$ome = floatval($appdata['ome']);
		$quan = floatval($appdata['quan']);
		$kuang = floatval($appdata['kuang']);
		$jifen = floatval($appdata['jifen']);
		if(($ome+$quan+$kuang+$jifen)==0 || $ome<0 || $quan<0 || $kuang<0 || $jifen<0){
			apifasong(['zt'=>'0','msg'=>'非法扣款额度']);
		}
		if(!isset($appdata['header']['uname'])){
			apifasong(['zt'=>'0','msg'=>'非法用户']);
		}
		$userinfo = userinfobyuname($appdata['header']['uname']);
		if(!$userinfo){
			apifasong(['zt'=>'0','msg'=>'用户不存在']);
		}
		$userinfo['secpwd']=$userinfo['secpwd']?$userinfo['secpwd']:MD5($setting['nopwd']);
		if(!isset($appdata['secpwd']) || md5($appdata['secpwd'])<>$userinfo['secpwd']){
			apifasong(['zt'=>'0','msg'=>'支付密码错误']);
		}
		if($userinfo['ome']<$ome || $userinfo['quan']<$quan || $userinfo['kuang']<$kuang || $userinfo['jifen']<$jifen){
			apifasong(['zt'=>'0','msg'=>'余额不足']);
		}
		//业务
		Db::startTrans();   // 启动事务-------------
		$ret = db('user')->where('id',$userinfo['id'])->dec('ome',$ome)->dec('quan',$quan)->dec('kuang',$kuang)->dec('jifen',$jifen)->update();
		if(!$ret){
			Db::rollback();     // 回滚事务-------------
			apifasong(['zt'=>'0','msg'=>'扣款失败']);
		}
		$oinfo = isset($appdata['ordersn'])?$appdata['ordersn']:'';
		if($ome){
			$log_ome = mylog('ome',$userinfo['uname'],'app','shop',$userinfo['ome'],'-'.$ome,'商城消费','','',$oinfo);
			$ret_ome1=ome1($userinfo,'-'.$ome);   //可提链OME1处理
			if(!$log_ome || !$ret_ome1){
				Db::rollback();     // 回滚事务-------------
				apifasong(['zt'=>'0','msg'=>'OME扣款失败']);
			}
		}
		if($quan){
			$log_quan = mylog('quan',$userinfo['uname'],'app','shop',$userinfo['quan'],'-'.$quan,'商城消费','','',$oinfo);
			if(!$log_quan){
				Db::rollback();     // 回滚事务-------------
				apifasong(['zt'=>'0','msg'=>'M币扣款失败']);
			}
		}
		if($kuang){
			$log_kuang = mylog('kuang',$userinfo['uname'],'app','shop',$userinfo['kuang'],'-'.$kuang,'商城消费','','',$oinfo);
			if(!$log_kuang){
				Db::rollback();     // 回滚事务-------------
				apifasong(['zt'=>'0','msg'=>'矿金扣款失败']);
			}
		}
		if($jifen){
			$log_jifen = mylog('jifen',$userinfo['uname'],'app','shop',$userinfo['jifen'],'-'.$jifen,'商城消费','','',$oinfo);
			if(!$log_jifen){
				Db::rollback();     // 回滚事务-------------
				apifasong(['zt'=>'0','msg'=>'积分扣款失败']);
			}
		}
		Db::commit();       // 提交事务-------------
		apifasong(['zt'=>'1','msg'=>'支付成功']);
    }
}
